package com.passion.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.passion.bean.OfflineAnalyseResult;
import com.passion.mapper.OfflineAnalyseResultMapper;
import com.passion.service.OfflineAnalyseResultTableService;
import com.passion.service.OfflineAnalysisTemplateSqlTableService;
import com.passion.utils.DateTimeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

import static com.passion.enums.CalculateStatus.DIRECT_CALCULATION_GET_RESULTS;

/**
 * @program offline-e-commerce-data-warehouse
 * @Description
 * @Author limeihua
 * @Date 2024/10/02 22:20
 * @Version 1.0
 */
@Service
public class OfflineAnalyseResultTableServiceImpl implements OfflineAnalyseResultTableService {

    @Autowired
    private OfflineAnalyseResultMapper offlineAnalyseResultMapper;

    @Autowired
    private OfflineAnalysisTemplateSqlTableService offlineAnalysisTemplateSqlTableService;

    @Override
    public JSONObject transformTableResult(Map<String, Object> param) {
        String analyseResult = null;
        JSONObject resultJson = new JSONObject();
        // 查询离线分析结果表，返回结果
        String sqlID = (String) param.get("sqlID");
        String date = (String) param.get("date");
        // 如果没有传入日期，默认为昨天
        if (date == null) {
            date = DateTimeUtil.timeStampToDate(System.currentTimeMillis() - 86400 * 1000);
        }
        OfflineAnalyseResult result = offlineAnalyseResultMapper.selectOne(new QueryWrapper<OfflineAnalyseResult>().eq("sql_id", sqlID).eq("analyse_time", date));
        if (result != null) {
            analyseResult = result.getAnalyseResult();
        } else {
            HashMap<String, Object> params = new HashMap<>();
            params.put("type", DIRECT_CALCULATION_GET_RESULTS);
            params.put("date", date);
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("sqlID", sqlID);
            params.put("params", jsonObject.toJSONString());
            offlineAnalysisTemplateSqlTableService.executeAnalysis(params);
            result = offlineAnalyseResultMapper.selectOne(new QueryWrapper<OfflineAnalyseResult>().eq("sql_id", sqlID));
            analyseResult = result.getAnalyseResult();
        }
        String fieldMap = (String) param.get("fieldMap");
        String[] split = fieldMap.split(",");
        JSONArray columnsArray = new JSONArray();
        for (String s : split) {
            JSONObject jsonObject = new JSONObject();
            String[] split1 = s.split(":");
            jsonObject.put("id", split1[0]);
            jsonObject.put("name", split1[1]);
            jsonObject.put("textAlign", "center");
            columnsArray.add(jsonObject);
        }
        resultJson.put("columns", columnsArray);
        JSONArray jsonArray = JSONArray.parseArray(analyseResult);
        resultJson.put("rows", jsonArray);
        return resultJson;
    }
}
